package com.jf3q.common.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.jf3q.common.system.Constant;
import com.jf3q.common.vo.ResultVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@Tag(name = "文件上传")
public class UploadController {

    @Value("${ali.endpoint}")
    private String endpoint;

    @Value("${ali.bucketName}")
    private String bucketName;

    @Value("${ali.accesskey}")
    private String accesskey;

    @Value("${ali.secretAccessKey}")
    private String secretAccessKey;
        @PostMapping("/oss/{type}")
        @Operation(summary = "oss上传文件")
        public ResultVo upload(@PathVariable String type, MultipartFile file) {

            if (file == null || file.isEmpty()){
                return ResultVo.error("文件必填");
            }

            String originalFilename = file.getOriginalFilename();

            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

            SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

            String dataPath = format.format(new Date());


            String fileName =dataPath+"/"+UUID.randomUUID().toString().replace("-","")+suffix;

            StringBuffer objectName = new StringBuffer();
            switch (type){
                case Constant.UploadType.AVATAR:
                    objectName.append(type+"/");
                    break;
                case Constant.UploadType.COVER:
                    objectName.append(type+"/");
                    break;
                case Constant.UploadType.BANNER:
                    objectName.append(type+"/");
                    break;
                case Constant.UploadType.VIDEO:
                    objectName.append(type+"/");
                    break;
                case Constant.UploadType.TEACHER:
                    objectName.append(type+"/");
                    break;
                default:
                    return ResultVo.error("参数不对");
            }

            objectName.append(fileName);

            // 填写本地文件的完整路径，例如D:\\localpath\\examplefile.txt。
            // 如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件。


            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accesskey,secretAccessKey);

            try {
                // 创建PutObjectRequest对象。
                PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName.toString(), file.getInputStream());
                // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
                // ObjectMetadata metadata = new ObjectMetadata();
                // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
                // metadata.setObjectAcl(CannedAccessControlList.Private);
                // putObjectRequest.setMetadata(metadata);

                // 上传文件。
                PutObjectResult result = ossClient.putObject(putObjectRequest);


                return ResultVo.success("",result);
            } catch (Exception oe) {
                return ResultVo.error("上传阿里云失败");
            }finally {
                if (ossClient != null){
                    ossClient.shutdown();
                }
            }

        }
}
